package com.tsd.core.version;

import com.alibaba.fastjson.JSON;
import com.tsd.core.constants.VersionConstants;
import com.tsd.core.utils.ExceptionUtil;
import com.tsd.core.utils.FileUtil;
import com.tsd.core.utils.HlpUtils;
import com.tsd.core.utils.ResourcesUtil;
import com.tsd.core.vo.VersionCfgVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 版本工具类
 * 2024-04-21 12:21:01
 *
 * @author Hillpool
 */
public class VersionUtil {

    public static final Logger LOGGER = LoggerFactory.getLogger(VersionUtil.class);

    private static Map<String, String> targetMap;

    /**
     * 初始化 targetMap
     *
     * @param vo
     */
    public static void initTargetMap(VersionCfgVo vo) {
        if (targetMap != null) {
            return;
        }
        targetMap = new ConcurrentHashMap<>();
        initTarget(VersionConstants.MODULE_VERSION_ULTIMATE, vo.getUltimate(), targetMap);
        initTarget(VersionConstants.MODULE_VERSION_PRO, vo.getPro(), targetMap);
        initTarget(VersionConstants.MODULE_VERSION_BASIC, vo.getBasic(), targetMap);
        initTarget(VersionConstants.MODULE_VERSION_FREE, vo.getFree(), targetMap);
    }

    /**
     * 加载验证信息
     *
     * @return
     */
    public static VersionCfgVo loadVersionCfgVo() {
        try {
            List<String> pathList = ResourcesUtil.getClassPath("classpath:tsd_version_config.json");
            if (!HlpUtils.isEmptyList(pathList)) {
                String json = FileUtil.readToString(pathList.get(0));
                return JSON.parseObject(json, VersionCfgVo.class);
            }
        } catch (Exception e) {
            LOGGER.error(ExceptionUtil.getStackTraceFromException(e));
        }
        return new VersionCfgVo();
    }

    /**
     * 根据目标路径获取版本
     *
     * @param target 目标路径
     * @return 版本
     */
    public static String verifyVersion(String target) {
        if (target == null) {
            return null;
        }
        if (targetMap == null) {
            VersionCfgVo vo = loadVersionCfgVo();
            initTargetMap(vo);
        }
        String version = targetMap.get(target);
        if (version == null) {
            version = VersionConstants.MODULE_VERSION_FREE;
        }
        return version;
    }

    private static void initTarget(String versionName, List<String> targets, Map<String, String> targetMap) {
        if (!HlpUtils.isEmptyList(targets)) {
            for (String url : targets) {
                targetMap.put(url, versionName);
            }
        }
    }

    public static void main(String[] args) {
        try {
            String ss = verifyVersion("/jxcSaleQuotation");
            System.out.println(ss);
            ss = verifyVersion("/jxcStockInBill");
            System.out.println(ss);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
